package cn.edu.fzu.homemaking.company.controller;

import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.common.SessionConstant;
import cn.edu.fzu.homemaking.company.domain.entity.CompanyServer;
import cn.edu.fzu.homemaking.company.domain.entity.CompanyUser;
import cn.edu.fzu.homemaking.company.domain.enums.ServerStatusEnum;
import cn.edu.fzu.homemaking.company.domain.enums.ServerPriceModeEnum;
import cn.edu.fzu.homemaking.company.service.CompanyServerService;
import cn.edu.fzu.homemaking.company.util.UserInfoUtil;
import cn.edu.fzu.homemaking.web.Result;
import cn.edu.fzu.homemaking.web.RetCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.edu.fzu.homemaking.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author wanjie
 * @version 1.0
 */
@RestController
@RequestMapping("/api/company/auth/server")
@Api(tags = "公司端服务需求相关接口")
public class CompanyServerController {
    @Resource
    private CompanyServerService companyServerService;

    @GetMapping("/list")
    @ApiOperation(value = "查询服务列表", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "companyId", value = "公司id"),
            @ApiImplicitParam(name = "labelId", value = "服务标签"), @ApiImplicitParam(name = "name", value = "服务名称"),
            @ApiImplicitParam(name = "chargeMode", value = "计费模式 0=按次数计费，1=按时长计费"),
            @ApiImplicitParam(name = "status", value = "状态,0=下架，1=上架"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(Long companyId, Long labelId, String name, ServerPriceModeEnum chargeMode,
                       ServerStatusEnum status, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        CompanyUser user = (CompanyUser) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        if (user == null && companyId == null) {
            throw new ServiceException("未登入，请登入");
        }
        if (user != null) {
            companyId = user.getCompany().getId();
        }
        try (Page<List<CompanyServer>> page = PageHelper.startPage(pageNum, pageSize)) {
            companyServerService.queryAllCompanyServers(companyId, labelId, name, chargeMode, status);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }


    @GetMapping("getById")
    @ApiOperation(value = "根据id获取服务", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "服务id", required = true) })
    Result getById(Long id, HttpServletRequest request) {
        CompanyUser user = (CompanyUser) request.getSession().getAttribute(SessionConstant.ACCOUNT);
        CompanyServer companyServer = companyServerService.getById(id);
        if (user != null) {
            Assert.isTrue(Objects.equals(user.getCompany().getId(), companyServer.getCompanyId()), "未查询到相关记录");
        }
        return new Result(companyServer);
    }


    @PostMapping("/insert")
    @ApiOperation(value = "新增服务", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "companyId", value = "公司id", required = true),
            @ApiImplicitParam(name = "labelId", value = "服务标签", required = true),
            @ApiImplicitParam(name = "name", value = "服务名字", required = true),
            @ApiImplicitParam(name = "chargeMode", value = "计费模式 0=按次数计费，1=按时长计费", required = true),
            @ApiImplicitParam(name = "price", value = "服务单价", required = true),
            @ApiImplicitParam(name = "status", value = "状态,0=下架，1=上架", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result insert(Long labelId, String name, ServerPriceModeEnum chargeMode, Double price,
                         ServerStatusEnum status, HttpServletRequest request) {
        CompanyUser user = UserInfoUtil.getUserInfo(request, false);
        companyServerService.insertCompanyServer(user.getCompany().getId(), labelId, name, chargeMode, price, status);
        return new Result(RetCode.SUCCESS, "新增服务成功");
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改服务信息", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", required = true),
            @ApiImplicitParam(name = "labelId", value = "服务标签"), @ApiImplicitParam(name = "name", value = "服务名字"),
            @ApiImplicitParam(name = "chargeMode", value = "计费模式 0=按次数计费，1=按时长计费"),
            @ApiImplicitParam(name = "price", value = "服务单价"),
            @ApiImplicitParam(name = "status", value = "状态,0=下架，1=上架", required = true)

    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateDemandStatus(Long id, Long labelId, String name, ServerPriceModeEnum chargeMode, Double price,
                                     ServerStatusEnum status) {
        companyServerService.updateCompanyServer(id, labelId, name, chargeMode, price, status);
        return new Result(RetCode.SUCCESS, "更新服务成功");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除服务", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "id", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result delete(Long id, HttpServletRequest request) {
        CompanyUser user = UserInfoUtil.getUserInfo(request, false);
        CompanyServer companyServer = companyServerService.getById(id);
        Assert.isTrue(Objects.equals(user.getCompany().getId(), companyServer.getCompanyId()), "未查询到相关记录");
        companyServerService.deleteCompanyServer(id);
        return new Result(RetCode.SUCCESS, "删除服务成功");
    }

}
